package com.zjl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjl.dto.SetMealDto;
import com.zjl.pojo.Category;
import com.zjl.pojo.Result;
import com.zjl.pojo.SetMeal;
import com.zjl.pojo.SetMealDish;
import com.zjl.service.CategoryService;
import com.zjl.service.SetMealDishService;
import com.zjl.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    @Autowired
    private SetMealDishService setMealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<SetMealDto>> page(int page, int pageSize, String name) {
        log.info("分页查询：page:{}, pageSize:{}, name:{}", page, pageSize, name);
        Page<SetMeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<SetMeal> setMealQueryWrapper = new LambdaQueryWrapper<>();
        setMealQueryWrapper.like(name != null, SetMeal::getName, name);
        setMealQueryWrapper.orderByDesc(SetMeal::getUpdateTime);
        setMealService.page(pageInfo,setMealQueryWrapper);

        Page<SetMealDto> pageDto = new Page<>();
        BeanUtils.copyProperties(pageInfo, pageDto, "records");
        List<SetMeal> records = pageInfo.getRecords();
        List<SetMealDto> setMealDtoList = records.stream().map(item -> {
            SetMealDto setMealDto = new SetMealDto();
            BeanUtils.copyProperties(item, setMealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category != null) {
                String categoryName = category.getName();
                setMealDto.setCategoryName(categoryName);
            }
            return setMealDto;
        }).collect(Collectors.toList());
        pageDto.setRecords(setMealDtoList);
        return Result.success(pageDto);
    }

    /**
     * 新增套餐
     * @param setMealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setMealCache", allEntries = true)
    public Result<String> save(@RequestBody SetMealDto setMealDto) {
        setMealService.saveWithDish(setMealDto);
        return Result.success("新增套餐成功！");
    }

    /**
     * 查询回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetMealDto> selectById(@PathVariable Long id) {
        log.info("id:{}",id);
        SetMealDto setMealDto = setMealService.getByIdWithDish(id);
        return Result.success(setMealDto);
    }

    /**
     * 修改套餐
     * @param setMealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setMealCache", allEntries = true)
    public Result<String> update(@RequestBody SetMealDto setMealDto) {
        log.info("setMealDto:{}",setMealDto.toString());
        setMealService.updateWithSetMealDish(setMealDto);
        return Result.success("修改成功！");
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value = "setMealCache", allEntries = true)
    public Result<String> changeStatus(@PathVariable("status") Integer status, @RequestParam List<Long> ids) {
        log.info("status:{}",status);
        LambdaQueryWrapper<SetMeal> setMealQueryWrapper = new LambdaQueryWrapper<>();
        setMealQueryWrapper.in(ids != null, SetMeal::getId, ids);
        List<SetMeal> setMealList = setMealService.list(setMealQueryWrapper);
        for(SetMeal setMeal : setMealList) {
            if(setMeal != null) {
                setMeal.setStatus(status);
                setMealService.updateById(setMeal);
            }
        }
        return Result.success("售卖状态修改成功！");
    }

    @DeleteMapping
    @CacheEvict(value = "setMealCache", allEntries = true)
    public Result<String> deleteWithSetMealDish(@RequestParam("ids") List<Long> ids) {
        log.info("ids:{}", ids);
        setMealService.removeWithSetMealDish(ids);
        return Result.success("删除成功！");
    }

    @GetMapping("/list")
    @Cacheable(value = "setMealCache", key = "#setMeal.categoryId + '_' + #setMeal.status")
    public Result<List<SetMeal>> selectWithSetMealDish(SetMeal setMeal) {
        log.info("setMeal:{}", setMeal);
        LambdaQueryWrapper<SetMeal> setMealQueryWrapper = new LambdaQueryWrapper<>();
        setMealQueryWrapper.eq(setMeal.getCategoryId() != null, SetMeal::getCategoryId, setMeal.getCategoryId());
        setMealQueryWrapper.eq(setMeal.getStatus() != null, SetMeal::getStatus, setMeal.getStatus());
        setMealQueryWrapper.orderByDesc(SetMeal::getUpdateTime);
        List<SetMeal> setMealList = setMealService.list(setMealQueryWrapper);
        return Result.success(setMealList);
    }
}
